package com.education.platform.management.service.impl;

import com.education.platform.common.exception.AssertException;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.SysUserDao;
import com.education.platform.dao.SysUserRoleDao;
import com.education.platform.dao.entity.SysUserDO;
import com.education.platform.dao.entity.SysUserRoleDO;
import com.education.platform.dao.model.SysUserModel;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.service.SysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* describe: 后台用户
* @author : 
* @date: 2020-06-30 09:05:21.722
**/
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserDO> implements SysUserService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
	private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertOrUpdate(SysUserDO userDO, List<Integer> roleIds) {
      this.checkUserName(userDO);

        if(userDO.getId()==null){
            userDO.setCreateBy(ThreadUser.getUserName());
            userDO.setCreateTime(new Date());
            userDO.setDeleted(false);
            userDO.setStatus(true);
            if(StringUtils.isEmpty(userDO.getPassWord())){
                userDO.setPassWord("123456");
            }
            sysUserDao.insert(userDO);

        }else{
            userDO.setUpdateBy(ThreadUser.getUserName());
            userDO.setUpdateTime(new Date());
            sysUserDao.updateById(userDO);
        }
        if(CollectionUtils.isNotEmpty(roleIds)){
            insertUserRole(userDO.getId(),roleIds);
        }
    }

    private void checkUserName(SysUserDO userDO){
        if(userDO == null){
            throw new BusinessException("数据为空");
        }
        if(StringUtils.isEmpty(userDO.getUserName())){
            throw new BusinessException("用户名为空");
        }
        SysUserDO query = new SysUserDO();
        query.setUserName(userDO.getUserName());
        SysUserDO exists = sysUserDao.get(query);
        if(exists!=null&&!exists.getId().equals(userDO.getId())){
            throw new BusinessException("用户名已存在");
        }


    }


    private void insertUserRole(Integer userId, List<Integer> roleIds) {
        sysUserRoleDao.deleteByUserId(userId);
        List<SysUserRoleDO> list = new ArrayList<>(roleIds.size());
        Date time = new Date();
        if(CollectionUtils.isNotEmpty(roleIds)){
            SysUserRoleDO userRoleDO;
            for(Integer roleId:roleIds){
                userRoleDO = new SysUserRoleDO();
                userRoleDO.setUserId(userId);
                userRoleDO.setRoleId(roleId);
                userRoleDO.setCreateTime(time);
                list.add(userRoleDO);
            }
            sysUserRoleDao.insertInBatch(list);
        }
    }

    @Override
    public void logicDelete(Integer id) {
        SysUserDO userDO =  new SysUserDO();
        userDO.setDeleted(true);
        sysUserDao.updateById(userDO);
    }


    @Override
    public List<SysUserModel> getByCondition(SysUserModel query) {
        return sysUserDao.getByCondition(query);
    }
}
